package com.hucheng.rest.interceptor;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-19
 * @Description: 请求会话拦截
 **/
@Slf4j
@Component
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        //校验头部信息
        if (request.getHeader("sign") == null) {
            throw new EduException(110008);
        }
        if (request.getHeader("action") == null) {
            throw new EduException(110005);
        }
        if (request.getHeader("userId") == null) {
            throw new EduException(100007);
        }
        if (request.getHeader("format") == null) {
            throw new EduException(110007);
        }
        if (!request.getHeader("action").equals("user_defaultAccessId")) {
            if (request.getHeader("accessId") == null) {
                throw new EduException(100005);
            }
        }

        if (request.getHeader("sign").isEmpty()) {
            throw new EduException(110008);
        }
        if (request.getHeader("action").isEmpty()) {
            throw new EduException(110005);
        }
        if (request.getHeader("userId").isEmpty()) {
            throw new EduException(100007);
        }
        if (request.getHeader("format").isEmpty()) {
            throw new EduException(110007);
        }
        if (!request.getHeader("action").equals("user_defaultAccessId")) {
            if (request.getHeader("accessId").isEmpty()) {
                throw new EduException(100005);
            }
        }

        String method = request.getMethod();
        if (!method.equalsIgnoreCase("post")) {
            throw new EduException(110006);
        }
        if (WebUtils.getInstance().getIP(request) == null || WebUtils.getInstance().getIP(request).equals("")) {
            throw new EduException(100009);
        }
        String api = request.getRequestURI();
        if (!api.equals("/rest/")) {
            throw new EduException(100010);
        }

        String path = request.getHeader("action");
        Entry entry = null;
        try {
            entry = SphU.entry(path, EntryType.IN, 1, request.getHeader("userId"));
            // Your logic here.
        } catch (BlockException ex) {
            // Handle request rejection.
            log.info(">>>>>>>> API访问次数限流 " + path + " 用户 " + request.getHeader("userId"));
            throw new EduException(100011);
        } finally {
            if (entry != null) {
                entry.exit(1, request.getHeader("userId"));
            }
        }
        log.info(">>>>>>>> API正常访问 " + path + " 用户 " + request.getHeader("userId"));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
